/*
 * Copyright (c) 2011 Anthony Vandikas
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation 
 * files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, 
 * modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software 
 * is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 
 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE 
 * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#pragma once

/**
 * Displays a message box containing relaying information to the user.
 *
 * @param info The message to display.
 */
void applicationInfo(const QString &info);

/**
 * Displays a message box containing an error message, then crashes the application.
 *
 * @param error The error message to display.
 */
void criticalError(const QString &error);

/**
 * Creates or opens a database of a specified name.
 * A table is created with the same name as the database (if none exist already)
 * using a provided string for determining it's parameters.
 *
 * @param name The name of the database and it's table.
 * @param tableString The string used to initialize the table.
 */
QSqlDatabase createDatabase(const QString &name, const QString &tableString);

/**
 * Safely opens a file using the specified flags, performs and operation on it,
 * then closes the file.
 *
 * @param filename The path to the file to open.
 * @param flag Indicates the file's access/use permissions.
 * @param func The action to perform on the file while it is open.
 */
template<typename T>
std::tuple<bool, T> useFile(const QString &filename, QFile::OpenModeFlag flag, const std::function<T (QFile *file)> func)
{
	QFile file(filename);

	if (!file.open(flag))
		return std::make_tuple(false, T());

	auto output = func(&file);
	file.close();
	
	return std::make_tuple(true, output);
}

/**
 * Safely opens a file using the specified flags, performs and operation on it,
 * then closes the file.
 *
 * @param filename The path to the file to open.
 * @param flag Indicates the file's access/use permissions.
 * @param func The action to perform on the file while it is open.
 */
template<typename T>
std::tuple<bool, T> useFileData(const QString &filename, QFile::OpenModeFlag flag, const std::function<T (QDataStream *stream)> func)
{
	return useFile<T>(
		filename,
		flag,
		[&](QFile *file) -> T
		{
			QDataStream stream(file);
			return func(&stream);
		});
}

/**
 * Safely opens a file using the specified flags, performs and operation on it,
 * then closes the file.
 *
 * @param filename The path to the file to open.
 * @param flag Indicates the file's access/use permissions.
 * @param func The action to perform on the file while it is open.
 */
template<typename T>
std::tuple<bool, T> useFileText(const QString &filename, QFile::OpenModeFlag flag, const std::function<T (QTextStream *stream)> func)
{
	return useFile<T>(
		filename,
		flag,
		[&](QFile *file) -> T
		{
			QTextStream stream(file);
			return func(&stream);
		});
}
